package com.atguigu.acl.controller;


import com.atguigu.acl.entity.Menu;
import com.atguigu.acl.entity.Permission;
import com.atguigu.acl.entity.Resource;
import com.atguigu.acl.entity.Role;
import com.atguigu.acl.service.RoleService;
import com.atguigu.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author smday
 * @since 2020-08-10
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation("添加角色")
    @PostMapping("/create")
    public Result create(@RequestBody Role role){
        int count = roleService.create(role);
        if (count > 0) {
            return Result.ok().data("count",count);
        }
        return Result.error();
    }

    @ApiOperation("修改角色")
    @PostMapping("/update/{id}")
    public Result update(@PathVariable Long id, @RequestBody Role role) {
        int count = roleService.update(id, role);
        if (count > 0) {
            return Result.ok().data("count",count);
        }
        return Result.error();
    }


    @ApiOperation("批量删除角色")
    @PostMapping("/delete")
    public Result delete(@RequestParam("ids") List<Long> ids) {
        int count = roleService.delete(ids);
        if (count > 0) {
            return Result.ok().data("count",count);
        }
        return Result.error();
    }

    @ApiOperation("获取相应角色权限")
    @GetMapping("/permission/{roleId}")
    public Result getPermissionList(@PathVariable Long roleId) {
        List<Permission> permissionList = roleService.getPermissionList(roleId);
        return Result.ok().data("permissionList",permissionList);
    }

    @ApiOperation("修改角色权限")
    @PostMapping("/permission/update")
    public Result updatePermission(@RequestParam Long roleId,
                                         @RequestParam("permissionIds") List<Long> permissionIds) {
        int count = roleService.updatePermission(roleId, permissionIds);
        if (count > 0) {
            return Result.ok().data("count",count);
        }
        return Result.error();
    }

    @ApiOperation("获取所有角色")
    @GetMapping("/listAll")
    public Result listAll() {
        List<Role> roleList = roleService.list();
        return Result.ok().data("roleList",roleList);
    }

    @ApiOperation("根据角色名称分页获取角色列表")
    @GetMapping("/list")
    public Result list(@RequestParam(value = "keyword", required = false) String keyword,
                                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Map<String,Object> map= roleService.list(keyword, pageSize, pageNum);
        return Result.ok().data(map);
    }

    @ApiOperation("修改角色状态")
    @PostMapping("/updateStatus/{id}")
    public Result updateStatus(@PathVariable Long id, @RequestParam(value = "status") Integer status) {
        Role role = new Role();
        role.setStatus(status);
        int count = roleService.update(id, role);
        if (count > 0) {
            return Result.ok().data("count",count);
        }
        return Result.error();
    }

    @ApiOperation("获取角色相关菜单")
    @GetMapping("/listMenu/{roleId}")
    public Result listMenu(@PathVariable Long roleId) {
        List<Menu> roleList = roleService.listMenu(roleId);
        return Result.ok().data("roleList",roleList);
    }

    @ApiOperation("获取角色相关资源")
    @GetMapping("/listResource/{roleId}")
    public Result listResource(@PathVariable Long roleId) {
        List<Resource> roleList = roleService.listResource(roleId);
        return Result.ok().data("roleList",roleList);
    }

    @ApiOperation("给角色分配菜单")
    @PostMapping("/allocMenu")
    public Result allocMenu(@RequestParam Long roleId, @RequestParam List<Long> menuIds) {
        int count = roleService.allocMenu(roleId, menuIds);
        if (count > 0) {
            return Result.ok().data("count",count);
        }
        return Result.error();
    }

    @ApiOperation("给角色分配资源")
    @PostMapping("/allocResource")
    public Result allocResource(@RequestParam Long roleId, @RequestParam List<Long> resourceIds) {
        int count = roleService.allocResource(roleId, resourceIds);
        if (count > 0) {
            return Result.ok().data("count",count);
        }
        return Result.error();
    }
}

